package DB
{
	public class DBSettings
	{
		/// DATABASE PART.......................................
		import flash.data.SQLConnection;
		import flash.data.SQLResult;
		import flash.data.SQLStatement;
		import flash.errors.SQLError;
		import flash.events.Event;
		import flash.events.SQLErrorEvent;
		import flash.events.SQLEvent;
		import flash.filesystem.File;
		import flash.events.KeyboardEvent;
		
		import forms.Utils;
		
		import mx.collections.ArrayList;
		import mx.events.CollectionEvent;
		import spark.components.View;
		
		//var dbFile:File = File.desktopDirectory.resolvePath("GibDB.db"); //ALWAYS COMMENT  BEFORE RELEASE!
		var dbFile:File = File.applicationStorageDirectory.resolvePath("GibDB.db");//SOS! Uncomment this before release!
		
		public function DBSettings()
		{
			
		}
		
		var dbTemplate:File = new File();
		private  var initConn = false;
		private  var conn:SQLConnection = new SQLConnection();
		private  var createStmt:SQLStatement = new SQLStatement();
		private  var selectStmt:SQLStatement = new SQLStatement();
		private  var insertStmt:SQLStatement = new SQLStatement();
		private  var deleteStmt:SQLStatement = new SQLStatement();
		public var result:SQLResult = new SQLResult();
		
		
		public function viewActivated_handler(evt:Event)
		{ 
			if(initConn)
			{
				
			}
		}
		
		public  function realInit()
		{
			if(conn.connected==true)
			{}
			else
			{
				try
				{
					conn.open(dbFile);
					tables();
				}
				catch (error:SQLError)
				{
					trace("Error message:", error.message);
					trace("Details:", error.details);
				}
			}	
		}
		
		public  function Init()
		{
			try
			{
				conn.open(dbFile);
				//tables();
				
			}
			catch (error:SQLError)
			{
				trace("Error message:", error.message);
				trace("Details:", error.details);
			}
		}
		
		
		public  function tables():void
		{
			
			createStmt.sqlConnection = conn;
			var sql:String = 
				"CREATE TABLE IF NOT EXISTS GibSettings (" + 
				"    uid INTEGER NOT NULL PRIMARY KEY, " + 
				"    lang TEXT " 	+
				")";
			//alter table name addColumn "complete TEXT"
			
			createStmt.text = sql;
			createStmt.execute();
			createStmt.addEventListener(SQLEvent.RESULT, createResult);
			createStmt.addEventListener(SQLErrorEvent.ERROR, createError);
		}
		
		
		public  function createError(event:SQLErrorEvent):void
		{
			trace("SQL tables error: "+event.error);
			//Alert.show("An error occured....");
		}
		
		public  function createResult(event:SQLEvent):void
		{}

		public var dataDG;
		
		public  function nowShow()
		{
			selectStmt.sqlConnection = conn;
			
			selectStmt.text = "SELECT * FROM GibSettings";
			
			// This try..catch block is fleshed out in
			// a subsequent code listing
			try
			{
				selectStmt.execute();
				// accessing the data is shown in a subsequent code listing
				result = selectStmt.getResult();
				
				
				return result;
				
				
				var index = 0;
				
				var obj:Object;
				//return result;
			}
			catch (error:SQLError)
			{
				trace("Error in now show: "+error.details);//Alert.show("no data for this month!");
			}
			
			
		}
		
		
		
		//adding gib ...
		public  function changeLang(lang)
		{
			//deleteNow();
			insertStmt.sqlConnection = conn;
			var today_date:Date = new Date();
			var sql:String = 
				"INSERT INTO GibSettings (uid, lang) " + 
				"VALUES (:uid, :lang)";
			insertStmt.text = sql;
			//insertStmt.parameters[":ID"] = add()
			
			insertStmt.parameters[":uid"] = 1000;
			insertStmt.parameters[":lang"] = lang;
			try
			{
				// execute the statement
				insertStmt.execute();
				
				//Alert.show("All done!");
				//nowShow();
				
			}
			catch (error:SQLError)
			{
				trace("Error occured on insert: "+error.details+">>"+error.message);//Alert.show((error.details).toString());
			}
		}
		
		
		
		
		////////////// DELETE/CHANGE FUNCTIONS ///////////////////////
		//////////////////////////////////////////////////////////////
		
		public  function deleteNow(uid,silent=false):void
		{
			deleteStmt.sqlConnection = conn;
			
			deleteStmt.text = "DELETE FROM GibSettings WHERE uid = :param1";
			deleteStmt.parameters[":param1"] = uid;
			
			// This try..catch block is fleshed out in
			// a subsequent code listing
			try
			{
				deleteStmt.execute();
			}
			catch (error:SQLError)
			{
				trace("Error on delete: "+error.details);//Alert.show("no data!");
			}
			if(!silent)
				nowShow();
		}
		
	}
}